package com.vincce.sport.core.utils;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;

import java.util.*;

/**
 * Created by longlong on 14-5-5.
 */
public class JsonUtil {

	/**
	 * 接送对象转成Map
	 * @param object
	 * @return
	 */
	public static Map<String,Object> jsonToMap(JSONObject object) {
		Map<String, Object> map = new HashMap<String,Object>();
		Set<String> jsonKeys = object.keySet();
		for (String key : jsonKeys) {
			Object jsonValObj = object.get(key);
			if(jsonValObj instanceof JSONArray) {
				map.put(key,arrayToMap((JSONArray)jsonValObj));
			} else {
				map.put(key,object.get(key));
			}
		}
		return map;
	}

	/**
	 * json数组转成Map
	 * @param jsonArray
	 * @return
	 */
	public static List<Map<String,Object>> arrayToMap(JSONArray jsonArray) {
		List<Map<String,Object>> list = new ArrayList<Map<String,Object>>();
		for (Object oject : jsonArray) {
			if(oject instanceof JSONObject) {
				list.add(jsonToMap((JSONObject)oject));
			}
		}
		return list;
	}










	public static void main(String args[]) {
		String ss = "{ \"msg_code\": \"0001\", \"msg_desc\": \"请求已完成！\", \"DATA\": [   {    \"Emp_Id\": \"010BED07-4DC1-4373-8643-943B6EA3E41A\",  \"Emp_Code\": \"D0672\",  \"Name\": \"王昉\"   }  ]}";
		JSONObject object = (JSONObject)JSONObject.parse(ss);
		Map<String,Object> map = jsonToMap(object);
		System.out.println(map);
	}


	/*public static Map<String,Object> jsonToMap(Map<String,Object> map, JSONObject object) {
		Set<String> jsonKeys = object.keySet();
		for (String key : jsonKeys) {
			Object jsonValObj = object.get(key);
			if(jsonValObj instanceof JSONObject) {
				jsonToMap(map,(JSONObject)jsonValObj);
			} else if(jsonValObj instanceof JSONArray) {
				arrayToMap(map,(JSONArray)jsonValObj);
			} else {
				map.put(key,object.get(key));
			}
		}
		return map;
	}

	public static Map<String,Object> arrayToMap(Map<String,Object> map, JSONArray jsonArray) {
		for(Object object:jsonArray) {
			if(object instanceof JSONArray) {
				arrayToMap(map,(JSONArray)object);
			} else if(object instanceof JSONObject) {
				jsonToMap(map,(JSONObject)object);
			}
		}
		return map;
	}*/

}
